package com.example.jack.community.persenter.login;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.example.jack.community.data.loginbean.RegisterStatus;
import com.example.jack.community.data.loginbean.RegisterVer;
import com.example.jack.community.data.loginbean.User;
import com.example.jack.community.model.login.RegisterView;
import com.example.jack.community.view.login.util.Md5;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * Created by TulinTonJie on 2017/10/21.
 */

public class RegisterPresenterlmp implements RegisterPresenter {

    private RegisterView registerView;

    private static final String TAG = "logins";

    private static String registerVerstring = null;

    private Context context;

    public RegisterPresenterlmp(Context context) {
        this.context = context;
    }

    /**
     * 注册逻辑
     *
     * @param user
     */
    @Override
    public void register(User user, String s1, String s2) {
        if (!TextUtils.isEmpty(user.getPhonenum()) && !TextUtils.isEmpty(user.getPassword()) && s2 != null) {
            if (s1.equals(s2)) {
                registerutil(user);
            } else {
                registerView.showToast("很抱歉，您两次输入的账户密码不一样，请重新输入");
            }
        } else {
            registerView.showToast("请输入要使用的用户名和密码");
        }
    }

       /**
     * 注册请求
     * 先进行get请求,获取接口中的字符串，进行md5加密，再截取字符串的20-30位，post过去
     *
     * @param user
     */
    private void registerutil(final User user) {

        String url;
        url = "http://192.168.1.11/register/";

        //Get请求获取字符串
        OkHttpUtils
                .get()
                .url(url)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.d(TAG, "onError: " + id);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        //对获取到的字符串，进行md5加密和截取操作


                        //Gson格式化接受到的json数据
                        Gson registerVer = new Gson();
                        RegisterVer registermd5 = registerVer.fromJson(response, RegisterVer.class);
                        Md5 register_md = new Md5();
                        String string;
                        string = register_md.md5(registermd5.getData());
                        registerVerstring = string.substring(20, 30);


                        //对截取后的加密字符串，post给后端
                        Md5 md5 = new Md5();
                        String password = user.getPassword().toString();
                        final String passwords = md5.md5(password);
                        Log.d(TAG, "onResponse: username:   " + user.getPhonenum() + "password:    " + passwords
                                + "telephone:   " + user.getPhonenum() + "   passcode:    " + registerVerstring);
                        OkHttpUtils
                                .post()
                                .url("http://192.168.1.11/register/")
                                .addParams("username", user.getPhonenum())
                                .addParams("password", passwords)
                                .addParams("telephone", user.getPhonenum())
                                .addParams("passcode", registerVerstring)
                                .build()
                                .execute(new StringCallback() {
                                    @Override
                                    public void onError(Call call, Exception e, int id) {

                                    }

                                    @Override
                                    public void onResponse(String response, int id) {
                                        Gson gson = new Gson();

                                        RegisterStatus shopInfo = gson.fromJson(response, RegisterStatus.class);

                                        shopInfo.getStatus();

                                        Log.d(TAG, "onResponse: "+response);
                                        if (shopInfo.getStatus().equals("0")) {
                                            registerView.registerSuccess("Registered successfully");
                                        }
                                        if (shopInfo.getStatus().equals("1")) {
                                            registerView.registerFail("Sorry, the user already exists");
                                        }
                                    }
                                });
                    }
                });
    }


    @Override
    public void attachView(RegisterView v) {
        this.registerView = v;
    }

    @Override
    public void detachView() {
        registerView = null;
    }

}
